package com.app.couponsharelife.ui.adapter

import androidx.databinding.DataBindingUtil
import androidx.constraintlayout.widget.ConstraintLayout
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.app.couponsharelife.R
import com.app.couponsharelife.data.bean.HomePageBean
import com.app.couponsharelife.databinding.HomeTypeItemBinding
import com.app.couponsharelife.manage.Contacts
import com.app.couponsharelife.ui.listener.OnClickListener
import com.app.couponsharelife.utils.AppUtils

/**
 * Created by guluwa on 2018/11/24.
 */
class HomeTypeAdapter(private val list: MutableList<HomePageBean.TypeBean>,
                      private val listener: OnClickListener) :
        androidx.recyclerview.widget.RecyclerView.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>() {

    fun setData(list: ArrayList<HomePageBean.TypeBean>) {
        this.list.clear()
        this.list.addAll(list)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        return ViewHolder(DataBindingUtil.inflate(
                LayoutInflater.from(parent.context), R.layout.home_type_item, parent, false))
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
        holder as ViewHolder
        Glide.with(holder.itemView.context).asBitmap()
                .load(Contacts.BASEURL + list[position].topType.thumb)
                .into(holder.databinding.ivTypeImage1)
        holder.databinding.tvSortType1.text = list[position].topType.bannername
        if (list[position].botType != null) {
            Glide.with(holder.itemView.context).asBitmap()
                    .load(Contacts.BASEURL + list[position].botType?.thumb)
                    .into(holder.databinding.ivTypeImage2)
            holder.databinding.tvSortType2.text = list[position].botType?.bannername
            holder.databinding.ivTypeImage2.visibility = View.VISIBLE
            holder.databinding.tvSortType2.visibility = View.VISIBLE
        } else {
            holder.databinding.ivTypeImage2.visibility = View.GONE
            holder.databinding.tvSortType2.visibility = View.GONE
        }
    }

    inner class ViewHolder(val databinding: HomeTypeItemBinding) : androidx.recyclerview.widget.RecyclerView.ViewHolder(databinding.root) {
        init {
            val width = (AppUtils.getDisplayMetrics(databinding.root.context).widthPixels * 0.936 * 0.2).toInt()
            databinding.root.layoutParams = ConstraintLayout.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT)
            databinding.mTopView.setOnClickListener {
                if (adapterPosition != -1) {
                    listener.click(0, list[adapterPosition].topType)
                }
            }
            databinding.mBotView.setOnClickListener {
                if (adapterPosition != -1 && list[adapterPosition].botType != null) {
                    listener.click(0, list[adapterPosition].botType!!)
                }
            }
        }
    }
}